//
//  OBMomentHeaderView.m
//  WeChat
//
//  Created by 黄柳姣 on 2018/5/2.
//  Copyright © 2018年 obally. All rights reserved.
//

#import "OBMomentHeaderView.h"
#import "OBMomentItemViewModel.h"
#import "OBMomentPhotosView.h"
@interface OBMomentHeaderView ()
/// 头像
@property (nonatomic, readwrite, weak) UIImageView *avatarView;
/// 昵称
@property (nonatomic, readwrite, weak) YYLabel *screenNameLable;
/// 正文
@property (nonatomic, readwrite, weak) YYLabel *contentLable;
/// 全文/收起 按钮
@property (nonatomic, readwrite, weak) UIButton *expandBtn;
/// 配图View
@property (nonatomic, readwrite, weak) OBMomentPhotosView *photosView;
/// viewModel
@property (nonatomic, readwrite, strong) OBMomentItemViewModel *viewModel;
/// upArrow
@property (nonatomic, readwrite, weak) UIImageView *upArrowView;
@end
@implementation OBMomentHeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"MomentHeader";
    OBMomentHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (header == nil) {
        //缓存池中没有，自己创建
        header = [[self alloc]initWithReuseIdentifier:ID];
    }
    return header;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        // 初始化
        [self _setup];
        
        // 初始化所有的子控件
        [self _setupSubViews];
        
        // 所有事件处理（PS：为了将UI和事件隔离）
        [self _dealWithAction];
    }
    return self;
}
- (void)_dealWithAction
{
    @weakify(self);
    ///正文点击事件
    [self.contentLable setHighlightTapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        /// 点击事件
        @strongify(self);
        if (range.location >= text.length) return;
        YYTextHighlight *highlight = [text attribute:YYTextHighlightAttributeName atIndex:range.location];
        NSDictionary *userInfo = highlight.userInfo;
        if (userInfo.count == 0) return;
        /// 回调数据
        [self.viewModel.attributedTapCommand execute:userInfo];
    }];
    /// 全文/收起 事件监听
    [[self.expandBtn rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(UIButton *sender) {
         @strongify(self);
         /// 更新子控件的frame
         [self.viewModel.expandOperationCmd execute:@(self.section)];
     }];
}
#pragma mark - 初始化
- (void)_setup
{
    self.contentView.backgroundColor = [UIColor whiteColor];
}
#pragma mark - 创建自控制器
- (void)_setupSubViews
{
    ///这里把所有可能要显示的View全部初始化好，避免滚动过程中的动态创建，影响性能
    
    ///用户头像
    UIImageView *avatarView = [[UIImageView alloc]init];
    avatarView.backgroundColor = self.contentView.backgroundColor;
    ///设置可交互
    avatarView.userInteractionEnabled = YES;
    self.avatarView = avatarView;
    [self.contentView addSubview:avatarView];
    
    ///昵称
    YYLabel *nickName = [[YYLabel alloc]init];
    nickName.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向中心对齐
    nickName.textVerticalAlignment = YYTextVerticalAlignmentCenter;
    /// 异步渲染和布局
    nickName.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    nickName.ignoreCommonProperties = YES;
    nickName.fadeOnAsynchronouslyDisplay = NO;
    nickName.fadeOnHighlight = NO;
    [self.contentView addSubview:nickName];
    self.screenNameLable = nickName;
    
    /// 正文
    YYLabel *contentLable = [[YYLabel alloc] init];
    contentLable.backgroundColor = self.contentView.backgroundColor;
    /// 垂直方向顶部对齐
    contentLable.textVerticalAlignment = YYTextVerticalAlignmentTop;
    /// 异步渲染和布局
    contentLable.displaysAsynchronously = NO;
    /// 利用textLayout来设置text、font、textColor...
    contentLable.ignoreCommonProperties = YES;
    contentLable.fadeOnAsynchronouslyDisplay = NO;
    contentLable.fadeOnHighlight = NO;
    contentLable.preferredMaxLayoutWidth = OBMomentCommentViewWidth();
    [self.contentView addSubview:contentLable];
    self.contentLable = contentLable;
    
    /// photosView
    OBMomentPhotosView *photosView = [[OBMomentPhotosView alloc] init];
    photosView.clipsToBounds = YES;
    photosView.backgroundColor = self.contentView.backgroundColor;
    self.photosView = photosView;
    [self.contentView addSubview:photosView];
    
    /// 展开、关闭按钮
    UIButton *expandBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [expandBtn setTitle:@"全文" forState:UIControlStateNormal];
    [expandBtn setTitleColor:OBMomentScreenNameTextColor forState:UIControlStateNormal];
    [expandBtn.titleLabel setFont:OBMomentExpandTextFont];
    [expandBtn setBackgroundImage:[UIImage imageWithColor:OBMomentTextHighlightBackgroundColor size:CGSizeMake(OBMomentExpandButtonWidth, OBMomentExpandButtonHeight)] forState:UIControlStateHighlighted];
    /// 子控件超出部分裁剪
    expandBtn.clipsToBounds = YES;
    [self.contentView addSubview:expandBtn];
    self.expandBtn = expandBtn;
    
    /// 向上的箭头
    UIImageView *upArrowView = [[UIImageView alloc] initWithImage:OBImageNamed(@"wx_albumTriangleB_45x6")];
    self.upArrowView = upArrowView;
    [self.contentView addSubview:upArrowView];
    
}
- (void)bindViewModel:(OBMomentItemViewModel *)viewModel
{
    self.viewModel = viewModel;
    /// 头像
    self.avatarView.frame = viewModel.avatarViewFrame;
    [self.avatarView setImageWithURL:[NSURL URLWithString:viewModel.moment.sender.avatar]  placeholder:OBDefaultAvatar(OBDefaultAvatarTypeDefualt) options:YYWebImageOptionAllowInvalidSSLCertificates|YYWebImageOptionAllowBackgroundTask completion:nil];
    /// 昵称
    self.screenNameLable.textLayout = viewModel.nickNameLableLayout;
    self.screenNameLable.frame = viewModel.screenNameLableFrame;
    
    /// 正文
    self.contentLable.textLayout = viewModel.contentLableLayout;
    self.contentLable.frame = viewModel.contentLableFrame;
    
    /// 全文/收起
    self.expandBtn.frame = viewModel.expandBtnFrame;
    [self.expandBtn setTitle:viewModel.isExpand?@"收起":@"全文" forState:UIControlStateNormal];
    
    /// 配图
    self.photosView.frame = viewModel.photosViewFrame;
    [self.photosView bindViewModel:viewModel];
    
    /// 箭头
    self.upArrowView.frame = viewModel.upArrowViewFrame;
    
}
@end
